package com.king.base.service.email.impl;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.king.base.auth.utils.EncryptUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.king.base.domain.email.EmailConfig;
import com.king.base.domain.menu.vo.EmailVo;
import com.king.base.exception.BadRequestException;
import com.king.base.mapper.email.EmailMapper;
import com.king.base.service.email.EmailService;
import com.king.base.utils.Constants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author TianYZ
 * @date 2019-06-16
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EmailServiceImpl extends ServiceImpl<EmailMapper,EmailConfig> implements EmailService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public EmailConfig update(EmailConfig emailConfig, EmailConfig old) {
        try {
            if(!emailConfig.getPass().equals(old.getPass())){
                // 对称加密
                emailConfig.setPass(EncryptUtils.desEncrypt(emailConfig.getPass()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (baseMapper.selectById("1")!=null){
            emailConfig.setId("1");
            baseMapper.updateById(emailConfig);
        }else {
            baseMapper.insert(emailConfig);
        }
        return emailConfig;
    }

    @Override
    public EmailConfig find() {
        Optional<EmailConfig> emailConfig = Optional.ofNullable(baseMapper.selectById(1L));
        if(emailConfig.isPresent()){
            return emailConfig.get();
        } else {
            return new EmailConfig();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void send(EmailVo emailVo, EmailConfig emailConfig){
        if(emailConfig == null){
            throw new BadRequestException("请先配置，再操作");
        }
        /**
         * 封装
         */
        MailAccount account = new MailAccount();
        account.setHost(emailConfig.getHost());
        account.setPort(Integer.parseInt(emailConfig.getPort()));
        account.setAuth(true);
        try {
            // 对称解密
            account.setPass(EncryptUtils.desDecrypt(emailConfig.getPass()));
        } catch (Exception e) {
            throw new BadRequestException(e.getMessage());
        }
        account.setFrom(emailConfig.getUser()+"<"+emailConfig.getFromUser()+">");
        //ssl方式发送
        account.setStartttlsEnable(true);
        String content = emailVo.getContent()+ Constants.EMAIL_CONTENT;
        /**
         * 发送
         */
        try {
            MailUtil.send(account,
                          emailVo.getTos(),
                          emailVo.getSubject(),
                          content,
                          true);
        }catch (Exception e){
            throw new BadRequestException(e.getMessage());
        }
    }

}
